﻿using System;
using System.IO;
using System.Windows.Forms;
using MG_POS_PC.Model;
using MG_POS_PC.BLL;
using System.Data;
using MG_POS_PC.Util;
using System.Drawing;

namespace MG_POS_PC
{
    public partial class UserSetting : BaseForm
    {
        private UserManage userManage;
        

        public UserSetting()
        {
            InitializeComponent();
            skinEngine1.SkinStream = memoryStreams[themeId];
        }

        private void writeLog(string mess)
        {
            FileStream fs = new FileStream(@"c:/mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" -->" + mess);
            m_streamWriter.Flush();
            m_streamWriter.Close();
            fs.Close();
        }

        private string getTextBox(TextBox textBox)
        {
            return textBox.Text.ToString().Trim();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.us_login_id = int.Parse(getTextBox(txtName));
            user.us_pwd = getTextBox(txtPassword);
            user.us_active = chbActive.Checked == true ? "Y" : "N";
            user.us_color = 1;
            user.us_manager = chbManager.Checked == true ? "Y" : "N";

            userManage = new UserManage();
            userManage.Add(user);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            
        }

        private void UserSetting_Load(object sender, EventArgs e)
        {
            
            Display();

            
            //this.listView1.ColumnClick += new ColumnClickEventHandler(listView1_ColumnClick);
            //this.listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
            //this.listView1.DrawItem += new DrawListViewItemEventHandler(listView1_DrawItem);
            //this.listView1.DrawSubItem += new DrawListViewSubItemEventHandler(listView1_DrawSubItem);
            //this.Controls.Add(this.listView1);
            //this.ResumeLayout();
        }

        private void Display()
        {
            listView1.Columns.Add("", 20);
            listView1.Columns.Add("us_id", 50);
            listView1.Columns.Add("us_login_id", 50);
            listView1.Columns.Add("us_pwd", 50);
            listView1.Columns.Add("us_info", 50);
            listView1.Columns.Add("us_active", 50);
            listView1.Columns.Add("us_color", 50);
            listView1.Columns.Add("us_manager", 50);

            DataTable dt = DBHelper.Query("select * from dbo.t_user_setting").Tables["ds"];
            listView1.Items.Clear();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                if (dr.RowState != DataRowState.Deleted)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Add(dr["us_id"].ToString());
                    lvi.SubItems.Add(dr["us_login_id"].ToString());
                    lvi.SubItems.Add(dr["us_pwd"].ToString());
                    lvi.SubItems.Add(dr["us_info"].ToString());
                    lvi.SubItems.Add(dr["us_active"].ToString());
                    lvi.SubItems.Add(dr["us_color"].ToString());
                    lvi.SubItems.Add(dr["us_manager"].ToString());
                    listView1.Items.Add(lvi);
                }
            }
        }

        private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            if ((e.ColumnIndex == 0))
            {
                CheckBox cck = new CheckBox();
                // With...
                Text = "";
                Visible = true;
                listView1.SuspendLayout();
                e.DrawBackground();
                cck.BackColor = Color.Transparent;
                cck.UseVisualStyleBackColor = true;

                cck.SetBounds(e.Bounds.X, e.Bounds.Y, cck.GetPreferredSize(new Size(e.Bounds.Width, e.Bounds.Height)).Width, cck.GetPreferredSize(new Size(e.Bounds.Width, e.Bounds.Height)).Width);
                cck.Size = new Size((cck.GetPreferredSize(new Size((e.Bounds.Width - 1), e.Bounds.Height)).Width + 1), e.Bounds.Height);
                cck.Location = new Point(3, 0);
                listView1.Controls.Add(cck);
                cck.Show();
                cck.BringToFront();
                e.DrawText((TextFormatFlags.VerticalCenter | TextFormatFlags.Left));

                cck.Click += new EventHandler(Bink);

                listView1.ResumeLayout(true);

            }
            else
            {
                e.DrawDefault = true;
            }
        }

        private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawDefault = true;
        }

        private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            e.DrawDefault = true;
        }

        private void Bink(object sender, System.EventArgs e)
        {
            CheckBox test = sender as CheckBox;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                listView1.Items[i].Checked = test.Checked;
            }
        }
    }
}
